package com.begye.smartloan.apptest.renzheng.dialog

import android.annotation.SuppressLint
import android.content.res.Resources
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaFillItemDateBinding
import com.begye.smartloan.apptest.ext.addColor
import com.begye.smartloan.apptest.tool.addTouch


class FillItemDateDialogFragment : DialogFragment() {

    companion object {
        const val TYPE_ONLY_YEAR = 1
        const val TYPE_YEAR_MONTH = 2
        const val TYPE_YEAR_MONTH_DAY = 0
    }

    private var _binding: DiaFillItemDateBinding? = null
    private val binding get() = _binding!!

    private var _clickListener: (String, String) -> Unit = { _, _ -> }
    private var _pickType = TYPE_YEAR_MONTH_DAY
    private var _pickTag: String = ""
    private var _title: String? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        val layout = inflater.inflate(R.layout.dia_fill_item_date, container, false)
        _binding = DiaFillItemDateBinding.bind(layout)
        return layout
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        binding.diaFillItemDateTitle.text = _title
        binding.diaFillItemDateTitle.addColor("*", "#F55555")

        when (_pickType) {
            TYPE_ONLY_YEAR -> {
                hideMonthAndDay()
            }

            TYPE_YEAR_MONTH -> {
                hideDay()
            }
        }

        //
        addTouch(type = "date_dialog_show")
    }

    private fun initializeView() {
        binding.diaFillItemDateConfirm.setOnClickListener {
            val year: Int = binding.diaFillItemDatePicker.year
            val month: Int = binding.diaFillItemDatePicker.month + 1
            val day: Int = binding.diaFillItemDatePicker.dayOfMonth
            val monthFormat = if (month < 10) "0$month" else month.toString()
            val dayFormat = if (day < 10) "0$day" else day.toString()
            val dateFormat = when (_pickType) {
                TYPE_ONLY_YEAR -> {
                    "$year"
                }

                TYPE_YEAR_MONTH -> {
                    "$year-$monthFormat-$dayFormat"
                }

                else -> {
                    "$year-$monthFormat-$dayFormat"
                }
            }

            _clickListener.invoke(_pickTag, dateFormat)
            _pickType = 0

            //
            addTouch(type = "date_dialog_yes_click")

            dismiss()
        }
    }

    fun addTitle(value: String): FillItemDateDialogFragment {
        _title = value
        return this
    }

    fun showWithTag(
        fm: FragmentManager,
        pickTag: String,
        pickType: Int = TYPE_YEAR_MONTH_DAY,
        callback: (String, String) -> Unit
    ) {
        this._pickTag = pickTag
        this._pickType = pickType
        this._clickListener = callback
        showNow(fm, pickTag)
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMonthAndDay() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = binding.diaFillItemDatePicker.findViewById(monthSpinnerId)
                monthSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }

        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = binding.diaFillItemDatePicker.findViewById(daySpinnerId)
                daySpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideYear() {
        try {
            val yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) {
                val yearSpinner: View? = binding.diaFillItemDatePicker.findViewById(yearSpinnerId)
                yearSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMonth() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = binding.diaFillItemDatePicker.findViewById(monthSpinnerId)
                monthSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideDay() {
        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = binding.diaFillItemDatePicker.findViewById(daySpinnerId)
                daySpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }
}